package com.xjgc.msibase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjgc.msibase.entity.MsiServer;
import com.xjgc.msibase.entity.MsiServerTask;
import com.xjgc.msibase.mapper.MsiServerMapper;
import com.xjgc.msibase.mapper.MsiServerTaskMapper;
import com.xjgc.msibase.service.IMsiServerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 中间库服务
 *
 * @Description:
 * @Author: 小逗号
 * @Date: 2023-11-02
 * @Version: V1.0
 */
@Slf4j
@Service
public class MsiServerServiceImpl extends ServiceImpl<MsiServerMapper, MsiServer> implements IMsiServerService {

    @Resource
    private MsiServerTaskMapper msiServerTaskMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(MsiServer msiServer, List<MsiServerTask> msiServerTaskList) {
        baseMapper.insert(msiServer);
        if(msiServerTaskList!=null && msiServerTaskList.size()>0) {
            for(MsiServerTask entity:msiServerTaskList) {
                //外键设置
                entity.setServerId(msiServer.getId());
                msiServerTaskMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(MsiServer msiServer,List<MsiServerTask> msiServerTaskList) {
        baseMapper.updateById(msiServer);

        //1.先删除子表数据
        msiServerTaskMapper.delete(new LambdaQueryWrapper<MsiServerTask>().eq(MsiServerTask::getServerId,msiServer.getId()));
//        msiServerTaskMapper.deleteByMainId(msiServer.getId());

        //2.子表数据重新插入
        if(msiServerTaskList!=null && msiServerTaskList.size()>0) {
            for(MsiServerTask entity:msiServerTaskList) {
                //外键设置
                entity.setServerId(msiServer.getId());
                msiServerTaskMapper.insert(entity);
            }
        }
    }
}
